package com.yjf.ljr.adapter;

import java.util.List;

import com.yjf.ljr.R;
import com.yjf.ljr.dataclass.BusinessManageDateClass.BusinessManageInfo;

import android.R.color;
import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class BusinessManageAdapter extends BaseAdapter {
	private Context mCtx;
	private List<BusinessManageInfo> mDate;
	private LayoutInflater mInflater;

	public BusinessManageAdapter(Context context, List<BusinessManageInfo> date) {
		this.mCtx = context;
		this.mDate = date;
		mInflater = LayoutInflater.from(context);

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mDate.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return mDate.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHold hold = null;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.item_business_manage, null);
			hold = new ViewHold();
			hold.mTvCommission = (TextView) convertView.findViewById(R.id.tvcommission);
			hold.mTvInvestor = (TextView) convertView.findViewById(R.id.tvinvestor);
			hold.mTvTradeName = (TextView) convertView.findViewById(R.id.tvtradingname);
			hold.mTvinvestdate = (TextView) convertView.findViewById(R.id.tvinvestdate);

			convertView.setTag(hold);
		} else

		{
			hold = (ViewHold) convertView.getTag();
			resetHold(hold);
		}
		final BusinessManageInfo item = mDate.get(position);
		// 佣金
		if (!(TextUtils.isEmpty(item.benefitAmount))) {
			hold.mTvCommission.setText("+ " + item.benefitAmount + "元");

			SpannableStringBuilder style = new SpannableStringBuilder(hold.mTvCommission.getText().toString());
			style.setSpan(new ForegroundColorSpan(Color.rgb(239, 134, 19)), 0, hold.mTvCommission.getText().toString()
					.length() - 1, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

			hold.mTvCommission.setText(style);
		}

		// 交易名稱
		if (!(TextUtils.isEmpty(item.tradeName))) {
			hold.mTvTradeName.setText("" + item.tradeName);
		}

		// 投资日期
		if (!(TextUtils.isEmpty(item.payDate))) {
			String[] array_item = item.payDate.split(" ");
			if (!TextUtils.isEmpty(array_item[0])) {
				hold.mTvinvestdate.setText(array_item[0]);
			} else {
				hold.mTvinvestdate.setText("" + item.payDate);
			}
		}
		// 投资人
		if (!(TextUtils.isEmpty(item.originalRealName))) {
			hold.mTvInvestor.setText("" + item.originalRealName);
		}
		return convertView;
	}

	private class ViewHold {

		TextView mTvCommission;// 佣金(元)
		TextView mTvInvestor;// 投资人
		TextView mTvTradeName;// 交易名称
		TextView mTvinvestdate;// 投资日期
	}

	private void resetHold(ViewHold hold) {

		hold.mTvCommission.setText("");
		hold.mTvInvestor.setText("");
		hold.mTvTradeName.setText("");
		hold.mTvinvestdate.setText("");

	}
}
